package com.yitongmao.activity;

import android.graphics.Color;
import android.os.Build;
import android.os.Bundle;
import android.support.v4.app.Fragment;
import android.support.v4.app.FragmentManager;
import android.support.v4.app.FragmentTransaction;
import android.support.v7.app.AppCompatActivity;
import android.view.View;
import android.view.Window;
import android.view.WindowManager;
import android.widget.RadioButton;

import com.yitongmao.custom.AutoRadioGroup;
import com.yitongmao.fragment.ClassifyFragment;
import com.yitongmao.fragment.HomeFragment;
import com.yitongmao.fragment.MyFragment;
import com.yitongmao.fragment.ShoppingFragment;
import com.yitongmao.util.DipUtils;
import com.yitongmao.yitmfruit.R;
import com.zhy.autolayout.AutoRelativeLayout;

import butterknife.Bind;
import butterknife.ButterKnife;
import butterknife.OnClick;

public class MainActivity extends AppCompatActivity {
    @Bind(R.id.rb_home)
    RadioButton rbHome;
    @Bind(R.id.rb_chat)
    RadioButton rbChat;
    @Bind(R.id.rb_found)
    RadioButton rbFound;
    @Bind(R.id.rb_my)
    RadioButton rbMy;
    @Bind(R.id.rg_home)
    AutoRadioGroup rgHome;
    @Bind(R.id.mAutoRelativeLayout)
    AutoRelativeLayout mAutoRelativeLayout;
    private FragmentTransaction fragmentTransaction;
    private FragmentManager fragmentManager;
    private ClassifyFragment mClassifyFragment;
    private HomeFragment mHomeFragment;
    private MyFragment mMyFragment;
    private ShoppingFragment mShoppingFragment;
    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        immerSion();
        setContentView(R.layout.activity_main);
        ButterKnife.bind(this);
        initView();
    }
    //初始化fragment
    private void initView() {
        mAutoRelativeLayout.setPadding(0,0,0, DipUtils.getBottomStatusHeight(this));
        mClassifyFragment = new ClassifyFragment();
        mHomeFragment = new HomeFragment();
        mMyFragment = new MyFragment();
        mShoppingFragment = new ShoppingFragment();
        //开始事物 默认一个视图
        addView(mHomeFragment);
        rbHome.setChecked(true);//设置默认选中
    }
    //点击切换不同的fragment
    @OnClick({R.id.rb_home, R.id.rb_chat, R.id.rb_found, R.id.rb_my, R.id.rg_home})
    public void onViewClicked(View view) {
        switch (view.getId()) {
            case R.id.rb_home:
                setView(mHomeFragment);
                break;
            case R.id.rb_chat:
                setView(mClassifyFragment);
                break;
            case R.id.rb_found:
                setView(mShoppingFragment);
                break;
            case R.id.rb_my:
                setView(mMyFragment);
                break;
        }
    }
    //点击替换不同的fragment
    public void setView(Fragment fragment) {
        fragmentManager = getSupportFragmentManager();
        fragmentTransaction = fragmentManager.beginTransaction();
        fragmentTransaction.replace(R.id.v4_drawerlayout_frame, fragment);
        fragmentTransaction.commit();
    }
    //刚开始进入首页的时候默认一个视图
    public void addView(Fragment fragment) {
        fragmentManager = getSupportFragmentManager();
        fragmentTransaction = fragmentManager.beginTransaction();
        fragmentTransaction.add(R.id.v4_drawerlayout_frame, fragment);
        fragmentTransaction.commit();
    }
    //沉浸式状态栏
    public void immerSion() {
        getWindow().requestFeature(Window.FEATURE_NO_TITLE);
        if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.LOLLIPOP) {
            Window window = getWindow();
            window.clearFlags(WindowManager.LayoutParams.FLAG_TRANSLUCENT_STATUS
                    | WindowManager.LayoutParams.FLAG_TRANSLUCENT_NAVIGATION);
            window.getDecorView().setSystemUiVisibility(View.SYSTEM_UI_FLAG_LAYOUT_FULLSCREEN
                    | View.SYSTEM_UI_FLAG_LAYOUT_HIDE_NAVIGATION
                    | View.SYSTEM_UI_FLAG_LAYOUT_STABLE);
            window.addFlags(WindowManager.LayoutParams.FLAG_DRAWS_SYSTEM_BAR_BACKGROUNDS);
            window.setStatusBarColor(Color.TRANSPARENT);
            window.setNavigationBarColor(Color.BLACK);
        }
    }
}

